package cn.com.taiji.pos.config;


import cn.com.taiji.pos.common.util.SmallTicketPrinter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration
public class CrossConfig implements WebMvcConfigurer {
    @Resource
    private SmallTicketPrinter smallTicketPrinter;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("http://localhost:8082", "http://localhost:8081", "http://localhost:8081", "http://demowx.aramark.cn", "http://*.aramark.cn")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("Origin", "No-Cache", "X-Requested-With", "If-Modified-Since", "Pragma", "Last-Modified", "Cache-Control", " Expires", "Content-Type", "X-E4M-With", "userId", "token", "Access-Control-Allow-Headers");
    }


    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.initialize();
        return taskScheduler;
    }

    @Scheduled(cron = "0 59 23 * * ?")
    public void clearPrintSerialNumber() {
        smallTicketPrinter.setPrintSerialNumber(new AtomicInteger(1));
    }
}